package com.invengo.library_rfid.analyze.utils.uhf.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ChangeDataType {
    public static byte[] convertHexToBytes(String value) {
        if (value.indexOf(" ") != -1) {
            value = value.replace(" ", "");
        }
        value = value.trim();
        if (!isHexadecimal(value)) {
            return null;
        }
        int num = (value.length() / 2);
        byte[] array = new byte[num];
        for (int i = 0; i < num; i++) {
            int index = i * 2;
            array[i] = (byte) Integer.parseInt(value.substring(index, index + 2), 16);
        }
        return array;
    }


    public static boolean isHexadecimal(String value) {
        Pattern pattern = Pattern.compile("^[0-9a-fA-F]+$");
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }
}
